# coding=utf-8
import arcpy
"""
json文件格式：
{
    "geometry":[
        [
          [103.04519499975, 29.6819379999388],
          [103.045186000234, 29.6820260003997],
          [103.045185000188, 29.6820929998921],
          [103.045189000372, 29.6821879997764],
          [103.045196999842, 29.6822819996146],
          [103.045209000395, 29.6823679999833],
          [103.045222784008, 29.6824461084995]
        ],
        [
          [103.045222784008, 29.6824461084995],
          [103.045224000188, 29.6824530003058],
          [103.045226945628, 29.6824651093553]
        ]
    ]
}
"""
def createPolylineFromJSON(JSONFile,OutFilePath):
    import json
    arcpy.env.overwriteOutput = True
    with open(JSONFile, 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)

    dataCount = len(data['geometry'])
    cnList = []
    for i in range(dataCount):
        pointCount = len(data['geometry'][i])
        # 点数少于50的删除
        if (pointCount > 50):
            for point in data['geometry'][i]:
                cnList.append(point)
    Lines = []
    Lines.append(arcpy.Polyline(arcpy.Array([arcpy.Point(*cn) for cn in cnList])))
    arcpy.CopyFeatures_management(Lines, OutFilePath)

def createPolygonFromJSON(JSONFile,OutFilePath):
    import json
    arcpy.env.overwriteOutput = True
    spatial_ref = arcpy.SpatialReference(4326)
    with open(JSONFile, 'rb') as file:
        content = file.read().decode('utf-8')
    data = json.loads(content)

    ArcpyPolygon_List = []
    for coordsList in data['geometry']:
        ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
        ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
        ArcpyPolygon = arcpy.Polygon(ArcpyArray_List,spatial_ref)
        ArcpyPolygon_List.append(ArcpyPolygon)
    arcpy.CopyFeatures_management(ArcpyPolygon_List, OutFilePath)